package com.hhs.storage.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.BasePermissionService;
import com.hhs.storage.service.ProjectPlanService;
import com.hhs.storage.util.DateEasy;
import com.hhs.storage.util.SysUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.sound.midi.SysexMessage;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hhs
 * @since 2023-01-13
 */
@RestController
@RequestMapping("/projectPlan")
@ApiSupport(order = 1)
@Api(tags = "项目计划")
public class ProjectPlanController {
    @Resource
    private ProjectPlanService projectPlanService;
    @Resource
    private BasePermissionService basePermissionService;
    @Resource
    private HttpServletRequest request;

    @ApiOperation(value = "得到登录的用户有无查权限",
            notes = "得到登录的用户有无查权限",
            httpMethod = "POST"
    )
    @ApiOperationSupport(order = 1)
    @RequestMapping("/hasPremission")
    public ResponseBean<Boolean> hasPremission() {
        boolean has = basePermissionService.hasPermission(SysUtil.getLoginUserId(request), "xmjhck");
        if (!has) {
            has = false;
        }
        ResponseBean res = ResponseBean.getInstance();
        res.setCode(1);
        res.setData(has);
        return res;
    }
    @RequestMapping("/initProjectCode")
    public ResponseBean<Boolean> initProjectCode() {
        String projectCode = new DateEasy().toYYYYMMDDHHMMSS()+"_"+SysUtil.getLoginUserId(request);
        ResponseBean res = ResponseBean.getInstance();
        res.setCode(1);
        res.setData(projectCode);
        return res;
    }
    /**
     * 根据id计划详细信息
     *
     * @param id
     * @return
     */

    @RequestMapping("/getById")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "根据id返回计划详细信息",
            httpMethod = "POST")
    @ApiImplicitParam(dataTypeClass = String.class, name = "id", value = "对应项目id", required = true, paramType = "query")
    public ResponseBean<ProjectPlan> getById(@RequestParam String id) {
        ResponseBean<ProjectPlan> res = ResponseBean.getInstance();
        Map<String, Object> map = projectPlanService.getDetailById(id);
        res.setCode(1);
        res.setData((ProjectPlan) map.get("data"));
        return res;
    }

    /**
     * 查询用户id相关的计划项目，如果没有用户id就查询当前登录用户的
     *
     * @param userId
     * @return
     */
    @RequestMapping("/getProjectPlansByUserId")
    public Map<String, Object> getProjectPlansByUserId(String userId, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate workDate) {
        return projectPlanService.getProjectPlansByUserId(userId, workDate);
    }

    /**
     * 查询计划信息
     *
     * @param projectPlan name 根据项目名查询
     * @param projectPlan state 根据项目名状态
     * @param projectPlan memberId 根据项目成员查询
     * @param inputParam  page,size 分页参数
     * @param inputParam  completeTimeStartDate completeTimeEndDate 根据项目完成时间的开始日期，结束日期查询
     * @return
     */
    @ApiOperation(value = "得到项目计划列表",
            notes = "得到项目计划列表",
            httpMethod = "POST"
    )
    @ApiOperationSupport(order = 2)
    @RequestMapping(value = "table")
    public ResponseBeanPage<ProjectPlan> getAll(@ApiIgnore ProjectPlan projectPlan, @ApiIgnore InputParam inputParam) {
        ResponseBean<Boolean> has = this.hasPremission();
        if (has.getData()) {
            inputParam.setPermission("xmjhck");
        }
        Map<String, Object> all = projectPlanService.getAll(projectPlan, inputParam);
        ResponseBeanPage res = ResponseBeanPage.getInstance();
        res.setCode(1);
        res.setTotal((Long) all.get("total"));
        res.setRows((List<ProjectPlan>) all.get("rows"));
        return res;
    }
    @PostMapping(value = "setFavorate")
    public Map<String, Object> setFavorate(InputParam inputParam) {
        return projectPlanService.setFavorate(inputParam);
    }
    @PostMapping(value = "edit")
    public Map<String, Object> addOrUpdate(@RequestBody ProjectPlan projectPlan) {
        return projectPlanService.addOrUpdate(projectPlan, projectPlan.getProjectMemberList(), projectPlan.getProjectWorkFlowList());
    }

    @PostMapping(value = "dele")
    public Map<String, Object> dele(String id) {
        try {
            return projectPlanService.dele(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseInfo.error("存在关联业务，无法删除");
        }

    }

    @PostMapping(value = "stateChange")
    public Map<String, Object> stateChange(String id, Integer state) {
        return projectPlanService.stateChange(id, state);
    }
    @PostMapping(value = "goFlow")
    public Map<String, Object> goFlow(InputParam inputParam ) {
        return projectPlanService.goFlow(inputParam);
    }
    @RequestMapping(value = "taskList")
    public Map<String, Object> taskList(InputParam inputParam) {
        return projectPlanService.taskList(inputParam);
    }

    /**
     * 查询所有项目成员指定日期项目计划
     *
     * @param projectPlanId
     * @param workDate
     * @return
     */
    @RequestMapping(value = "memberTaskList")
    public Map<String, Object> memberTaskList(@RequestParam String projectPlanId, @RequestParam String workDate) {
        return projectPlanService.memberTaskList(projectPlanId, workDate);
    }

    /**
     * 按照最近完成工序排序的项目记录
     *
     * @param inputParam
     * @return
     */
    @RequestMapping(value = "getRecentCompletePlan")
    public Map<String, Object> getRecentCompletePlan(InputParam inputParam) {
        return projectPlanService.getRecentCompletePlan(inputParam);
    }

    /**
     * 获取当前个人项目进度
     *
     * @param id
     * @return
     */
    @PostMapping(value = "getPersonProgress")
    public Map<String, Object> getPersonProgress(@RequestParam String id) {
        return projectPlanService.getPersonProgress(id);
    }

    /**
     * 编辑当前个人项目进度
     *
     * @param id
     * @param progress
     * @return
     */
    @PostMapping(value = "editPersonProgress")
    public Map<String, Object> editPersonProgress(@RequestParam String id, @RequestParam Integer progress) {
        return projectPlanService.editPersonProgress(id, progress);
    }

    /**
     * 项目统计
     *
     * @param param id  部门id
     * @param param name 用户名
     * @param param month 月分
     * @return
     */
    @RequestMapping(value = "statisticalByUser")
    public Map<String, Object> statisticalByUser(InputParam param) {
        return projectPlanService.statisticalByUser(param);
    }

    /**
     * 查询用户指定月份项目奖金
     *
     * @param userId 用户id，没有就是当前用户
     * @param month  月份数据 2023-06
     * @return
     */
    @RequestMapping(value = "getProjectBonusByMonth")
    public Map<String, Object> getProjectBonusByMonth(String userId, @RequestParam String month, InputParam param) {
        return projectPlanService.getProjectBonusByMonth(userId, month, param);
    }

    @RequestMapping("/getMemberById")
    public Map<String, Object> getMemberById(String projectId, String userId) {
        return projectPlanService.getMemberById(projectId, userId);
    }

}

